package server;

import hibernate.HibernateUtil;

import java.util.List;

import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import api.Person;

/**
 * @author Ann Raboschuk
 */
public class Server implements IAmServer
{
	private static String configLocation="server_config.xml";
	public static void main(String args[]) 
	   { 
			ApplicationContext context = new
			ClassPathXmlApplicationContext(configLocation);

			context.getBean("rmservice");
			
			System.out.println("Server is ready."); 
	   } 
	
	public Server(){}
	public Server(String configLocation)
	{
		Server.configLocation=configLocation;
	}
	
   @Override
   public Long add(Person person)
   {
      Long id=0l;
	  Session session = null;
      try
      {
         session = HibernateUtil.getSessionFactory().openSession();
         session.beginTransaction();         
         id=(Long) session.save(person);                 
         session.getTransaction().commit();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      finally
      {
         if (session != null && session.isOpen())
         {
            session.close();
         }
      }
      return id;
   }

   @Override
   public void delete(Person person)
   {
      Session session = null;
      try
      {
         session = HibernateUtil.getSessionFactory().openSession();
         session.beginTransaction();
         session.delete(person);
         session.getTransaction().commit();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      finally
      {
         if (session != null && session.isOpen())
         {
            session.close();
         }
      }
   }

   @Override
   public void edit(Person person)
   {
      Session session = null;
      try
      {
         Long id;
    	 session = HibernateUtil.getSessionFactory().openSession();
         session.beginTransaction();
//         Person pOld=(Person)session.get(Person.class, person.getId());
//         id=pOld.getId();
//         session.getTransaction();
//         session.beginTransaction();
//         person.setId(id);
         session.update(person);
         session.getTransaction().commit();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      finally
      {
         if (session != null && session.isOpen())
         {
            session.close();
         }
      }
   }

   @SuppressWarnings("unchecked")
   @Override
   public List<Person> loadAll()
   {
      Session session = null;
      List<Person> persons = null;
      try
      {
         session = HibernateUtil.getSessionFactory().openSession();
         persons = session.createCriteria(Person.class).list();
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
      finally
      {
         if (session != null && session.isOpen())
         {
            session.close();
         }
      }
      return persons;
   }
}
